package com.mashibing.juc.c_026_00_Interview.A1B2C3;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

/**
 * 两个线程把自己的char放到Queue里面，让对方去拿去打印。
 * @author: Yuanzt
 * @Date: 2021/4/2
 */
public class T12_TransferQueue {

    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        TransferQueue<Character> queue = new LinkedTransferQueue<>();

        new Thread(()->{
            try {
                for (char c : aI) {
                    System.out.print(queue.take());//先去Queue里面拿
                    queue.transfer(c);//往Queue里面放
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        new Thread(()->{
            try {
                for (char c : aC) {
                    queue.transfer(c);//往Queue里面放
                    System.out.print(queue.take());//去Queue里面拿
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();
    }

}
